import { hasScope } from '@/utils/auth'

/**
 * 模板替换
 * @param template
 * @param params
 * @returns {*}
 */
export const tplReplace = (template, params) => {
  const regex = /\{\$(\w*[:]*[=]*\w+)\}(?!})/g
  return template.replace(regex, function(match, key) {
    if (params[key] === null || params[key] === undefined) {
      return ''
    }
    if ((key === 'cost_price' || key === 'cost_amount') && !hasScope('stock_cost')) {
      return '**'
    }
    return params[key]
  })
}
